// xc_lshop/shop/deposit/take/index.js
var actmemberbill = require('../../../../xc/model/member')
var actdepositbill = require('../../model/deposit_bill')
const app = getApp()

Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: app.globalData.CustomBar,
    navindex: 0,
    allpros: [],
    form: {
      uid: 0,
      data: {}
    }
  },
  getorder(id) {
    var that = this;
    actdepositbill.getorder(id).then(res => {
      if (res["status"] == 1) {
        that.setData({
          order: res['obj'],
          form: res['obj']['contents']['form']
        })
        if (res['obj'].status == 1) {
          console.log("ssss")
          that.oncomfirm(res['obj']['contents']['form'].data, res['obj']['contents']['modellist'])
        } else {
          var statusname = "已拒绝"
          if (res['obj'].status == 2) {
            statusname = "已取件"
          }
          wx.showModal({
            title: "提示",
            content: "该订单" + statusname + ",是否重新取件？",
            confirmText: '重新取件',
            cancelText: '返回首页',
            success(res) {
              if (res.confirm) {
                that.setData({
                  depositid: "",
                  order: {},
                  'form.data': {}
                })

              } else if (res.cancel) {
                wx.redirectTo({
                  url: '/xc_lshop/index/index',
                })
              }
            }
          })
        }
      } else {
        that.error(res['message'])
      }
    })
  },
  oncomfirm(formdata, list) {

    if (!formdata || (formdata && formdata.type)) {
      formdata = this.data.form.data;
    }
    if (!list) {
      var allpros = this.data.allpros
      console.log("sdsssssssss")
      // console.log(allpros)
      console.log(formdata)

      list = []
      for (var i in allpros) {
        if (formdata[(allpros[i]['proid'] + "_" + allpros[i]['protype'])]) {
          list.push(allpros[i])
        }
      }
      console.log("list")
      console.log(list)
    }
    console.log("list")
    console.log(list)

    if (list.length == 0) {
      wx.showToast({
        icon: "none",
        title: '请选择您要取件的商品',
      })
      return
    }

    this.selectComponent("#checkpros").init(list, this.data.form)
  },
  onChange(e) {
    console.log(e)
    this.setData({
      navindex: e.detail.index
    })

    this.setData({
      'modellist': []
    })

    this.changepro(e.detail.index)
  },
  changepro(index) {
    var update = {}
    var allpros = this.data.allpros;
    if (index == 0) {
      update['modellist'] = allpros
    } else {
      var newlist = []
      for (var i in allpros) {
        if (allpros[i]['protype'] == index) {
          newlist.push(allpros[i])
        }
      }
      update['modellist'] = newlist;
    }
    this.setData(update)

  },
  xcloaddata: function (options, config) {
    if (config.deposit && config.deposit.status == 1) {
      this.setData({
        depositconfig: config.deposit
      })
    } else {
      this.error("寄存功能未开启", true)
    }

  },
  getmember(uid) {
    var that = this;
    actmemberbill.getmember(uid).then(res => {
      if (res["status"] == 1) {
        that.setData({
          member: res['obj']
        })
        this.getpros(res['obj'].member_id)
      } else {
        that.error(res['message'])
      }
    })
  },
  afterlogin: function (options, config, userinfo) {
    var that = this;
    var update = {}
    update['form.uid'] = options.uid

    if (userinfo['member_id'] != 0) {
      if (options.uid == userinfo['member_id'] && userinfo.memberinfo.authority != 2) {
        update['member'] = userinfo
        update['operator'] = 'user'
        this.getpros(userinfo.member_id)
      } else {
        update['operator'] = 'manager'
        if (userinfo.memberinfo.authority == 2) {
          update['manager'] = userinfo
          this.getmember(options.uid)
          if (options.depositid) {
            update['depositid'] = options.depositid
            this.getorder(options.depositid)
          }
        } else {
          that.error("抱歉，只有管理员才可以操作", true)
        }
      }
    }
    this.setData(
      update
    )
  },
  getpros(uid) {
    var that = this;
    actdepositbill.getpros(uid).then(function (res) {
      if (res["status"] == 1) {
        console.log(res['obj'])
        that.setData({
          allpros: res['obj'],
          modellist: res['obj']
        })
      } else {
        wx.showModal({
          title: '错误',
          content: res["message"],
        })
      }
    })
  },
  add(e) {
    var dataset = e.currentTarget.dataset
    var proid = dataset.proid
    var protype = dataset.protype
    var pro_ident = dataset.ident;
    var form = this.data.form;
    var key = proid + "_" + protype
    if (!form.data[key]) {
      form.data[key] = {}
    }
    if (!form.data[key][pro_ident]) {
      form.data[key][pro_ident] = 1
    } else {
      form.data[key][pro_ident] += 1
    }
    this.setData({
      form: form
    })
  },
  onChangestep(e) {
    console.log("e")
    console.log(e)
    var dataset = e.currentTarget.dataset
    var proid = dataset.proid
    var protype = dataset.protype
    var ident = dataset.ident
    var key = proid + "_" + protype

    var form = this.data.form
    if (e.detail <= 0) {
      delete form.data[key][ident];
      if (Object.keys(form.data[key]).length == 0) {
        delete form.data[key]
      }
    } else {
      form.data[key][ident] = e.detail
    }
    this.setData({
      form: form
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})